﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using DBAccessor.Accessor;
using AlanUtilities;
using DBList.Entities;
using DBList;
using System.Collections.Generic;

public partial class NullifyLiJuan : System.Web.UI.Page
{
	protected void Page_Load(object sender, EventArgs e)
	{
		String strUID = OperateControl.PickupAvaiableUserID(this);
		if (!IsPostBack)
		{
			int nUID, nOperateID;
			int.TryParse(strUID, out nUID);
			MemberAccessor memberSystem = new MemberAccessor();
			UserData usrData = memberSystem.GetMemberInfo(nUID);
			if (usrData.GroupId != 1 && usrData.GroupId != 2)
			{
				Session[@"LogUserId"] = null;
				OperateControl.PickupAvaiableUserID(this);
				return;
			}
			nOperateID = nUID;
			operaterLabel.Text = strUID;

			BindDrpCategory(nOperateID);

			SelKindDrpCategory(0);
		}
	}
	protected void BindDrpCategory(int nOperateID)
	{
		ddlGiftKind.Items.Add(new ListItem(@"请选择种类", "0"));
		CPNAccessor accessor = new CPNAccessor();
		List<ClassData> list = accessor.GetAllKind(nOperateID);
		foreach (ClassData clsData in list)
		{
			ddlGiftKind.Items.Add(new ListItem(clsData.ClassName, clsData.ClassId.ToString()));
		}
	}
	private void SelKindDrpCategory(int nKindID)
	{
		ClassData selclsData = null;
		CPNAccessor accessor = new CPNAccessor();
		if (nKindID != 0)
		{
			ClassData clsData = accessor.GetGoodsKindInfo(nKindID);
			foreach (ListItem li in ddlGiftKind.Items)
			{
				if (li.Text == clsData.ClassName && li.Value == clsData.ClassId.ToString())
				{
					ddlGiftKind.SelectedValue = li.Value;
					selclsData = clsData;
					break;
				}
			}
		}
		if (selclsData == null)
		{
			ListItem ddlLI = ddlGiftKind.SelectedItem;
			nKindID = int.Parse(ddlLI.Value);

			selclsData = accessor.GetGoodsKindInfo(nKindID);
		}
		if (selclsData != null)
		{
			tbSingleMoney.Text = selclsData.ClassMoney.ToString();

			tbBeginNum.Text = "";
			tbTotalNum.Text = "";
			tbEndNum.Text = "";
		}
	}
	protected void ddlGiftKind_SelectedIndexChanged(object sender, EventArgs e)
	{
		SelKindDrpCategory(0);
	}
	protected void btnSubmit_Click(object sender, EventArgs e)
	{
		Label2.Text = @"";
		string strBeginInt = tbBeginNum.Text.Trim();
		string strEndInt = tbEndNum.Text.Trim();
		int nBeginInt = 0, nEndInt = 0;
		int.TryParse(strBeginInt, out nBeginInt);
		int.TryParse(strEndInt, out nEndInt);
		if (nEndInt < nBeginInt || nBeginInt < 1)
		{
			tbTotalNum.Text = "";
			Label2.Text = @"结束数必需比开始数大且开始数大于0！";
			return;
		}
		int nTotalNum = nEndInt - nBeginInt + 1;
		tbTotalNum.Text = nTotalNum.ToString();
		ListItem ddlLI = ddlGiftKind.SelectedItem;
		int	nKindID;
		nKindID = int.Parse(ddlLI.Value);
		if (nKindID == 0)
		{
			Label2.Text = @"请选择作废礼券种类！";
			return;
		}
		string strComment = tbComment.Text.Trim();

		int nOpeID = int.Parse(operaterLabel.Text);
		CPNAccessor accessor = new CPNAccessor();
		int nRet = accessor.NullifyBarCode(nOpeID, nKindID, nBeginInt, nEndInt, strComment);
		if(nRet < 0){
			Label2.Text = @"作废失败！";
		}
		else if(nRet == 20){
			Response.Redirect("NullifyDetails.aspx");
		}
	}
}
